/**
 * @Author: yuanmanxue
 * @Date:   2018-01-12 10:14:13
 * @Last modified by:   yuanmanxue
 * @Last modified time: 2018-01-12 03:20:51
 */
//【参考链接】:https://github.com/dwqs/blog/issues/17
{
  var myObject = {
    foo: "bar",
    func: function() {
        var self = this;
        console.log("outer func:  this.foo = " + this.foo);//bar
        console.log("outer func:  self.foo = " + self.foo);//bar
        (function(test) {
            console.log("inner func:  this.foo = " + test.foo);//bar
            console.log("inner func:  self.foo = " + self.foo);//bar
        }(self));
        // (function() {
        //     console.log("inner func:  this.foo = " + this.foo);//报错  Cannot read property 'foo' of undefined
        //     console.log("inner func:  self.foo = " + self.foo);
        // }());
    }
};
  myObject.func();
}

{
  /**
   * [addFn description] 用来处理number浮点数类型相加，默认会转化成二进制在计算，从而存在四舍五入误差。用升幂降幂的方法计算
   * @param {[type]} num1 [description]
   * @param {[type]} num2 [description]
   */
  function addFn(num1,num2){
    let r1,r2, m;
    r1 = (''+num1).split('.')[1].length;
    r2 = (''+num2).split('.')[1].length;
    m = Math.pow(10,Math.max(r1,r2));
    return (num1*m+num2*m)/m
  }
  console.log(addFn(0.1,0.256655));
}
{
  /**
   * [isInteger description]判断是否是一个整数;es6里面存在Number.isInteger()方法用来判断是否是一个整数
   * @param  {[type]}  num [description]
   * @return {Boolean}     [description]
   */
  // function isInteger(num){
  //   return parseInt(num,10) === num
  // }
  // isInteger(100)//true
  // isInteger(112.5)//false
  console.log(Number.isInteger(112.5));
  //在整数这个范围里面，2^53~-2^53 范围里面的整数都是属于安全范围内的整数，但是在范围之外的整数是不能够精确表示的，属于不安全整数，用Number.MAX_SAFE_INTEGER和Number.MIN_SAFE_INTEGER来表示整数的最大限度和最小限度，用Number.isSafeInteger来检测是否在安全整数范围之内；
  console.log(Number.MAX_SAFE_INTEGER);
  console.log(Number.MIN_SAFE_INTEGER);
  console.log(Number.isSafeInteger(-900719925474099111));//false
}
{
  /**
   * [isPalindrome description]判断是否是回文字符串   level aabbaa 类似
   * @param  {[type]}  str [description]
   * @return {Boolean}     [description]
   */
  function isPalindrome(str){
    str = str.replace(/\M/g,'').toLowerCase();
    return str == str.split('').reverse().join('');
  }
  console.log(isPalindrome('加油加'));
}

{
  /**
   * [sum description]   函数sum(2,3) sum(2)(3) 这两种写法都能够返回 5
   * @return {[type]} [description]
   */
  function sum(){
    let len = arguments.length;
    let fir = arguments[0];
    if(len === 2) {
      return arguments[0]+arguments[1]
    }else{
      return function(b){
        return b+fir
      }
    }
  }
  console.log(sum(1,2));//3
  console.log(sum(1)(2));//3
  //升级一下如果是多个这样的，该怎么处理；
  function sum2(a){
    let len = arguments.length;
    let result = arguments[0];
    let ret = 0;
    if(len > 1){
      for(let item of arguments){
        ret+=item
      }
      return ret;
    } else {}
  }
  console.log(sum2(1,2,3,4,5));
  // console.log(sum2(1)(2)(3)(4)(5));
}
{
  var arr1 = "john".split(''); //j o h n
  var arr2 = arr1.reverse(); //n h o j
  console.log(arr1);
  console.log(arr2);
  var arr3 = "jones".split('');// j o n e s
  arr2.push(arr3);
  console.log(arr1);//["n", "h", "o", "j", ["j", "o", "n", "e", "s"]]  arr1 == arr2 因为数组是引用类型 因为arr1.reverse()返回的是原来的数组 所以引用的是同一个对象
  console.log(arr2);//["n", "h", "o", "j", ["j", "o", "n", "e", "s"]]
  console.log(arr1.slice(-1)); //取最后一位["j", "o", "n", "e", "s"]
  console.log("array 1: length=" + arr1.length + " last=" + arr1.slice(-1));
  console.log("array 2: length=" + arr2.length + " last=" + arr2.slice(-1));
}
{
  //数字字符串之前存在数字中的正负号(+/-)时，会被转换成数字
  console.log(1 +  "2" + "2");//122
  console.log(1+ +'2');//3
  console.log(1 +  +"2" + "2");//32
  console.log(1 +  -"1" + "2");//02
  console.log(+"1" +  "1" + "2");//112
  console.log( "A" - "B" + "2");//NaN2
  console.log( "A" - "B" + 2);//NaN
}
{
    for (var i = 0; i < 5; i++) {
      // 1.IIFF
    // (function(i){
    //   setTimeout(function() { console.log(i); }, i * 1000 );
    // })(i)
    // 2. 把var改成let  for(let i ...)循环在每次迭代的时候都为x创建新的绑定
  }
}
{
  console.log("0 || 1 = "+(0 || 1));// 0 || 1 = 1
  console.log("1 || 2 = "+(1 || 2));// 1 || 2 = 1
  console.log("0 && 1 = "+(0 && 1));// 0 && 1 = 0
  console.log("1 && 2 = "+(1 && 2));// 1 && 2 = 2
  //逻辑与返回第一个是 false 的操作数 或者 最后一个是 true的操作数
  console.log('1:'+ (15 && 2)); //2
  //逻辑或返回第一个是 true 的操作数 或者 最后一个是 false的操作数
  console.log('2:'+ (5 || 2)); //5

  console.log('3:'+ (0 || 2 && 1));//1
  console.log('4:'+ (1 && 5 || 1));//5
}
{
  if({}){
    //true
    console.log('{}  true');
  }
  if([]){
    //true
    console.log('[] true');
  }
  console.log({} == false);//false
  console.log([] == false);//ture
  console.log({} === false);//false

  console.log(false == '0')//ture
  console.log(false === '0')  //false
}

{
  var a={},
    b={key:'b'},
    c={key:'c'};
    a[b]=123;
    a[c]=456;
    console.log(a[b]);//456
}
{
  console.log((function f(n){return ((n > 1) ? n * f(n-1) : n)})(10));  //10!  3628800  是10的阶层  10*9*8*7*6*5*4*3*2*1
}

{
    var hero = {
      _name: 'John Doe',
      getSecretIdentity: function (){
          return this._name;
      }
  };
  // var stoleSecretIdentity;
  // stoleSecretIdentity.call(this,getSecretIdentity)
  //   console.log(stoleSecretIdentity());//报错  stoleSecretIdentity全局变量是上下文
   console.log(hero.getSecretIdentity());
}
